package com.zhuqi.dynamic.config;

import com.github.pagehelper.PageInterceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;
import java.util.Properties;

/**
 * @author : ZhuQi
 * @version : 时间:2022/5/6 13:58 版本:
 */
@Configuration
@MapperScan(basePackages = {"com.zhuqi.dynamic.ds3.mapper"},sqlSessionFactoryRef = "Ds3SqlSessionFactory")
public class Ds3MybatisConfiguration {

    @Bean(name = "db3")
    @ConfigurationProperties(prefix = "spring.datasource.ds3")
    public DataSource dataSource3() {
        return DataSourceBuilder.create().build();
    }

    @Bean
    public SqlSessionFactory Ds3SqlSessionFactory() throws Exception {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(dataSource3());
        factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*Mapper.xml"));
        PageInterceptor pageInterceptor = new PageInterceptor();
        Properties properties = new Properties();
        properties.setProperty("helperDialect", "mysql");
        properties.setProperty("reasonable", "true");
        properties.setProperty("supportMethodsArguments", "true");
        properties.setProperty("params", "count=countSql");
        pageInterceptor.setProperties(properties);
        factoryBean.setPlugins(pageInterceptor);

        return factoryBean.getObject();

    }

    @Bean
    public SqlSessionTemplate sqlSessionTemplateReport3() throws Exception {
        return new SqlSessionTemplate(Ds3SqlSessionFactory());
    }

}
